package com.dksidana.java.jdbc.core;


import java.sql.Connection;
import java.sql.SQLException;

import com.dksidana.java.jdbc.JDBCUtils;

public abstract class AbstractQueryRunner implements QueryRunner{

	public <T> T executeQuery(String query, ResultSetHandler<T> rsh)
			throws SQLException {
		Connection connection=null;
		try {
			connection=getConnection();
			return JDBCUtils.executeQuery(connection, query, rsh);
		} catch (SQLException e) {
			handleException(e, connection);
			throw e;			
		}
		finally
		{
			closeConnection(connection);
		}
		
	}

	abstract Connection getConnection();
	abstract void handleException(SQLException exception,Connection connection);
	abstract void closeConnection(Connection connection);
	
	
}
